﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace _8___URLWithGroups
{
    class Program
    {
        static void ExtractURL(string input)
        {
            string pattern = @"(?<protokol>https?://)?(?<server>(www\.)?([\w\&\?=-]+\.)+[A-z]{2,4})(?<resource>(\/([\w\&\?=-]+)(\.[\w\&\?=-]+)*)*)?";
           
            Regex r = new Regex(pattern, RegexOptions.IgnoreCase);
            Match m = r.Match(input);
            while (m.Success)
            {
                Group group = m.Groups["protokol"];
                if(group.Value!="")
                Console.WriteLine("[protokol] " + group);
                group = m.Groups["server"];
                if (group.Value != "")
                Console.WriteLine("[server] " + group);
                group = m.Groups["resource"];
                if (group.Value != "")
                Console.WriteLine("[resource] " + group);
                Console.WriteLine();
                Console.WriteLine();
                m = m.NextMatch();
            }            
        }

        static void Main(string[] args)
        {
            string text = "Here are : some : @ ! url with http or without and some : abv.bg is this a url? " +
            "here is a valid url http://regexlib.mamaia.com/DisplayPatterns/kjasdhf.aspx?cattabindex=1&categoryId=2 " +
            "https://www.codeplex.com";
            ExtractURL(text);
        }
    }
}
